import type { IStack } from "../../types";

/**
 * @description: 使用对象模拟栈;LIFO;时间复杂度O(n)
 * @author hzq
 * @date 2025-4-10 12:54:00
 */
class Stack<T> implements IStack<T> {
  private items: T[];
  constructor() {
    this.items = [];
  }

  isEmpty(): boolean {
    return this.items.length === 0;
  }

  clear(): void {
    this.items = [];
  }

  size(): number {
    return this.items.length;
  }

  peek() {
    return this.items[this.items.length - 1];
  }

  push(...items: T[]): void {
    this.items.push(...items);
  }

  pop() {
    return this.items.pop();
  }

  toString() {
    return this.size() === 0
      ? null
      : `Stack [${this.items[this.items.length - 1]},...rest]`;
  }
}

export default Stack;
// const s = new Stack();
// s.push(1);
// s.push(2);
